package com.ruoyi.web.controller.manage;

import com.ruoyi.system.domain.Category;
import com.ruoyi.system.domain.vo.CategorySearchRequest;
import com.ruoyi.system.domain.vo.CategorySearchResult;
import com.ruoyi.system.domain.vo.Result;
import com.ruoyi.system.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/tree")
    public Result<List<Category>> getCategoryTree() {
        return Result.success(categoryService.getCategoryTree());
    }

    @PostMapping
    public Result<Void> addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> updateCategory(@PathVariable Long id, @RequestBody Category category) {
        category.setId(id);
        categoryService.updateCategory(category);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }
    @PostMapping("/search")
    public Result<List<CategorySearchResult>> search(@RequestBody CategorySearchRequest request) {
        List<CategorySearchResult> results = categoryService.searchCategories(request.getKeyword());
        return Result.success(results);
    }
}
